<?php

namespace App\Business\BLL;

use App\Models\ChallengeGame;
use Carbon\Carbon;

class ChallengeGameBLL
{
    public function startGame($playA, $playB = null, $type, $time = null, $status = 10)
    {
        $game = new ChallengeGame;
        $vaildA = $playA->validateChallengeA();
        if ($vaildA['status']) {
            $game->play_a = $playA->id;
        } else {
            return $vaildA;
        }
        $vaildB = $playB->validateChallengeB();
        if ($vaildB['status']) {
            $game->play_b = $playB->id;
        } else {
            return $vaildB;
        }

        if ($game->play_a == $game->play_b) {
            return ['status' => false, 'msg' => '不能和自己对战'];
        }
        if ($time == null) {
            $time = Carbon::now();
        }
        $game->city = $playA->city;
        $game->type = $type;
        $game->game_datetime = $time;
        $game->status = $status;

        $game->save();
        return ['status' => true, 'id' => $game->id, 'msg' => '比赛创建成功'];

    }
}
